<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>

<!-- vue -->
<script src="/assets/js-vue/vue.js"></script>
<!--element-->
<link rel="stylesheet" type="text/css" href="/assets/js-vue/element/index.css">
<!--<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">-->
<script src="/assets/js-vue/element/index.js"></script>
<!-- jQuery-->
<script type="text/javascript" src="/assets/jquery-3.6.1.min.js"></script>

<script type="text/javascript" src="/utils/common.js"></script>
<!--请求封装-->
<script type="text/javascript" src="/utils/myAjax.js"></script>

</head>

<body>
<div id="app">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
        <el-form-item label="用户账号" prop="name">
            <el-input
                    v-model="queryParams.name"
                    placeholder="请输入用户账号"
                    clearable
                    @keyup.enter.native="list()"
            />
        </el-form-item>
        <el-form-item label="性别" prop="sex" >
            <el-select v-model="queryParams.sex" clearable  placeholder="请选择性别">
                <el-option
                        v-for="item in [{value:'0',label:'男'},{value:'1',label:'女'},{value:'2',label:'中性'}]"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="list()">搜索</el-button>
        </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" style="margin-bottom: 10px">
        <el-col :span="1.5">
            <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="toAdd()"
            >新增</el-button>
        </el-col>
    </el-row>
    <el-row style="margin-bottom: 15px">
        <el-table v-loading="loading" :data="tableData" style="width: 100%;margin-bottom: 15px">
            <!--<el-table-column type="selection" width="55" align="center" />-->
            <!--<el-table-column label="用户ID" align="center" prop="userId" />
            <el-table-column label="密码" align="center" prop="password" />-->
            <el-table-column
                    prop="name"
                    label="账号"
                    align="center">
            </el-table-column>
            <el-table-column label="生日"  align="center" prop="birth" ></el-table-column>
            <el-table-column label="性别"  align="center" prop="sex" >
                <template slot-scope="scope">
                     <span
                      v-if="[{value:'0',label:'男'},{value:'1',label:'女'},{value:'2',label:'中性'}].filter(s=> {return s.value == scope.row.sex}).length > 0 "
                    >{{[{value:'0',label:'男'},{value:'1',label:'女'},{value:'2',label:'中性'}].find(s =>{ return  s.value == scope.row.sex }).label }}</span>
                    <span v-else> 未知</span>
                </template>
            </el-table-column>
            <el-table-column label="地址"  align="center" prop="addr" ></el-table-column>
            <el-table-column label="操作"  align="center" >
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="toEdit(scope.row)"
                    >修改</el-button>
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="toDel(scope.row)"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                style="display: flex;justify-content: end;margin-right: 100px"
                background
                layout="prev, pager, next"
                :total="page.total"
                :current-page.sync="page.current"
                :page-size.sync="page.size"
                @current-change="list()"
        >
        </el-pagination>
    </el-row>

    <!-- 添加或修改用户信息对话框 -->
    <el-dialog title="操作框" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" label-width="80px">
            <el-form-item label="账号" prop="userName">
                <el-input v-model="form.name" placeholder="请输入用户账号" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
            <el-input v-model="form.password" placeholder="请输入密码" />
            </el-form-item>
            <el-form-item label="地址" prop="addr">
                <el-input v-model="form.addr" placeholder="请输入地址" />
            </el-form-item>
            <el-form-item label="出生日期" prop="birth">
                <el-date-picker
                        v-model="form.birth"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="性别" prop="sex">
                <el-select v-model="form.sex" placeholder="请选择性别">
                    <el-option
                            v-for="item in [{value:'0',label:'男'},{value:'1',label:'女'},{value:'2',label:'中性'}]"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="handleSubmit()">确 定</el-button>
            <el-button @click="()=>{this.open =false;}">取 消</el-button>
        </div>
    </el-dialog>
</div>

<script type="text/javascript ">
    var app = new Vue({
        el:"#app",
        data(){
            return{
                loading:false,
                tableData:[],
                open:false,
                queryParams:{
                    name:undefined,
                    sex:undefined
                },
                page:{
                    total:0,
                    size:10,
                    current:1,
                },
                form:{},
                formLabelWidth:'120px',
            }
        },
        created(){
            this.list();
        },
        methods:{
           list(){
               let that = this;
               that.loading = true;
               sendAjaxNoToken("/user/list","POST",this.queryParams).then((res)=>{
                        if(res.data && res.data.length > 0 ){
                            const start = (this.page.current -1) * this.page.size;
                            const end = (this.page.current -1) * this.page.size + this.page.size;
                            /*console.log(start)
                            console.log(end)*/
                            that.tableData = res.data.slice(start,end);
                            /*console.log('=====')
                            console.log(this.tableData)*/
                            that.page.total = res.data.length;
                            that.loading = false;
                        }else {
                            that.tableData = [];
                            that.page.total = 0;
                            that.loading = false;
                        }
                    })
            },
            toAdd(){
                this.open = true;
                this.form = {};
            },
            handleSubmit(){
                if(this.form.id >= 0){
                    //修改
                    sendAjaxNoToken("/user/edit","POST",this.form).then((res)=>{
                        if(res.msg == '操作成功'){
                            this.$message.success("修改成功");
                            this.list();
                            this.open = false;
                        }else{
                            this.$message.error(res.msg);
                        }
                    })
                }else{
                    //新增
                    sendAjaxNoToken("user/add","POST",this.form).then((res)=>{
                        if(res.msg == '操作成功'){
                            this.$message.success("新增成功");
                            this.list();
                            this.open = false;
                        }else{
                            this.$message.error(res.msg);
                        }
                    })
                }
            },
            toEdit(row){
                sendAjaxNoToken("/user/getInfo","POST",row).then((res)=>{
                    this.form = res.data;
                    this.open = true;
                })
            },
            toDel(row){
                sendAjaxNoToken("/user/del","POST",row).then((res)=>{
                    if(res.msg == '操作成功'){
                        this.$message.success("删除成功");
                        this.list();
                    }else{
                        this.$message.error(res.msg);
                    }

                })

            },
        }


    })
</script>

</body>
</html>